<?php
error_reporting(E_ALL);

ini_set('display_errors', '0');

$a = session_id();if(empty($a)) session_start();
include_once dirname(__FILE__).'/class/functions.php';
require_once dirname(__FILE__).'/class/database.class.php';
require_once dirname(__FILE__).'/class/user.class.php';
$db=new stdb();

$logat=is_logat();
if(!$logat) die("Trebuie sa fii logat pentru a face o rezervare. <a href='index.php'> Go back</a>");
///////////////////////////////
$show="";
$search="<div id='search_rez'>";
$perioade=  pt_rezervari("perioade", "perioada");
$sali=  pt_rezervari("sali", "sala");

///////////////////////// default search ////////////////////////////
if(!isset($_GET['action'])){
    $user=unserialize($_SESSION['userc']);
        $query="SELECT sali.nume,perioade.perioada,rezervari.data ,detalii_utilizatori.Nume,detalii_utilizatori.Prenume,rezervari.motiv FROM `rezervari` INNER JOIN `sali` ON rezervari.OID=sali.SID INNER JOIN `perioade` ON rezervari.PID=perioade.PID INNER JOIN `detalii_utilizatori` ON rezervari.UID=detalii_utilizatori.ID WHERE RID>0 AND STATUS=1 AND data >= '".date("Y-m-d", time())."' ";
        
        $query.="  ORDER BY data ASC LIMIT 0,100";
        $result=$db->q($query);
        $search.= "<table border='1'>";
        $search.= "<tr border='1'>";
         $search.=  "<td>";
                $search.=  "Sala";
                $search.=  "</td>";
                 $search.=  "<td>";
                $search.=  "Interval Orar";
                $search.=  "</td>";
                 $search.=  "<td>";
                $search.=  "Data";
                $search.=  "</td>";
                 $search.=  "<td>";
                $search.=  "Nume";
                $search.=  "</td>";
                 $search.=  "<td>";
                $search.=  "Prenume";
                $search.=  "</td>";
                 $search.=  "<td>";
                $search.=  "Motiv";
                $search.=  "</td>";
        $search.=  "</tr>";
        while($rows = mysql_fetch_array($result)){
            $search.= "<tr>";
            //print_r($rows);
           /// foreach($rows as $row){
            for($i=0;$i<6;$i++){
                $search.=  "<td>";
                $search.=  $rows[$i];
                $search.=  "</td>";
            }
            $search.=  "</tr>";
            
                   }
         $search.= "</table>";
    
}

///////////////////////////
if(isset($_GET["actiune"])){
    if(strcmp($_GET["actiune"],"add")==0){
       // print_r($_POST);
        //print_r($_SESSION);
        mysql_safer();
        if(strlen($_POST['data'])>4 && strlen($_POST['perioada'])>0 && strlen($_POST['sala'])>0){
        $user=unserialize($_SESSION['userc']);
        $query="INSERT INTO `rezervari` (`RID`, `UID`, `OID`, `PID`, `motiv`, `data`) VALUES (NULL, '$user->id', '".$_POST['sala']."', '".$_POST['perioada']."', '".$_POST['motiv']."', '".$_POST['data']."')";
        $db->q($query);
        $show="<p> Rezervarea inregistrata - in curand un administrator o va vizializa! </p> <br />";
        }
        else {
            $show="<p> Date invalide! </p> <br />";
        }
        
    }
    
    if(strcmp($_GET["actiune"],"list")==0){
        
        //print_r($_SESSION);
        mysql_safer();$do=0;
        if(strlen($_POST['data'])>4) $do=1;
        if( strlen($_POST['perioada'])>1) $do=1;
        if(strlen($_POST['sala'])>0) $do=1;
        if(strlen($_POST['motiv'])>4) $do=1;
        if($do>0){
        $user=unserialize($_SESSION['userc']);
        $query="SELECT sali.nume,perioade.perioada,rezervari.data ,detalii_utilizatori.Nume,detalii_utilizatori.Prenume,rezervari.motiv FROM `rezervari` INNER JOIN `sali` ON rezervari.OID=sali.SID INNER JOIN `perioade` ON rezervari.PID=perioade.PID INNER JOIN `detalii_utilizatori` ON rezervari.UID=detalii_utilizatori.ID WHERE RID>0";
        
        $query.="  ORDER BY data DESC";
        $result=$db->q($query);
        $search.= "<table border='1'>";
        $search.= "<tr border='1'>";
         $search.=  "<td>";
                $search.=  "Sala";
                $search.=  "</td>";
                 $search.=  "<td>";
                $search.=  "Interval Orar";
                $search.=  "</td>";
                 $search.=  "<td>";
                $search.=  "Data";
                $search.=  "</td>";
                 $search.=  "<td>";
                $search.=  "Nume";
                $search.=  "</td>";
                 $search.=  "<td>";
                $search.=  "Prenume";
                $search.=  "</td>";
                 $search.=  "<td>";
                $search.=  "Motiv";
                $search.=  "</td>";
        $search.=  "</tr>";
        while($rows = mysql_fetch_array($result)){
            $search.= "<tr>";
            //print_r($rows);
           /// foreach($rows as $row){
            for($i=0;$i<6;$i++){
                $search.=  "<td>";
                $search.=  $rows[$i];
                $search.=  "</td>";
            }
            $search.=  "</tr>";
            
                   }
         $search.= "</table>";
      // /$show="<p> Rezervarea inregistrata - in curand un administrator o va vizializa! </p> <br />";//
        }
        else {
            $show="<p> Data invalida sda! </p> <br />";
        }
        
    }
    
}
$search.="</div>";
{
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="icon" href="favicon.jpg" type="image/gif" sizes="16x16" /> 
        <link rel="stylesheet" type="text/css" href="./style/style.css" />
        <title>Rezervari Sali</title>
    </head>
<body>
   <? header_out(); 
      footer();
        ?>
        <div id="content">
            <div id="content_inside">
                    <div id="content_inside_sidebar">
                        <ul>
                            <li><a href ="index.php"> Home</a><br />
                            </li><li><a href ="cauta.php"> Cautare</a><br /></li>
                            <li><a href ="rezervare.php"> Rezervari</a><br /></li>                   
                                           
                         </ul>
                                 
                    </div>
            <div id="content_inside_main">
                <? echo $show; ?>
<form  action="rezervare.php?actiune=list" method=post>
                    <?
                    echo $perioade;
                    echo $sali;
                    
                    ?>
                    Motiv: <input type="text" name="motiv" value=""/> 
                    Data: <input type="text"  id="datepicker" name="data" value=""/> <br/>
                    <a href="#" class="grey-button pcb"><span><input type="Submit" value="submit"></input></span></a> <br/> <br/>
 </form>
             <form>
                    <input type="radio" name="mod" class="sel_mod" value="0" /> Cautare<br />
                    <input type="radio" name="mod" class="sel_mod" value="1" /> Adaugare<br />
             </form>  
<hr /><br />

<?
echo $search;
//print_r($_POST);
//print_r($_GET);
?>

            </div> 
<?
}


?>
<script type="text/javascript" src="js/js_scrollv.js"></script>
<script type="text/javascript">
setSlider($('#scroll-pane'));
</script>
</body>



</html>